var num = $("#modalLarge").find(".moneycard").length; //红包数
$(".react.btn-block").eq(2).find(".more-after").text(num); //显示红包数量


var myfun = {

    //初始化，调用所有方法
    my_init: function () {
        this.change_nav_color();
        this.show_indent();
        this.check_page();
        this.check_content();
        this.check_money_style();
        this.touchmove_delide();
        this.once = true;    //是否执行一次
        this.show_dingdan_num();
        this.use_hongbao();
        //使用红包


    },

    //所有订单页面的函数
    //  change  nav_list   color
    change_nav_color: function() {
        var that = this;
        $('#tablist dt').on('click', function() {
            that.hide_delide_btn();    //隐藏订单的删除键
            var n = $(this).index();   //按钮
            var m = n - 1;             //状态

            $(this).addClass('active').siblings('dt').removeClass('active');
            if(n == 0) {
                if($(".order-item").length == 1){
                    $(".order-item").show();
                    $('#total_indent').find('.modal-body').css('background','#f0f0f0');
                }else{
                    $(".order-item").siblings().show();
                    if($(".order-item").length == 0){
                        $('#total_indent').find('.modal-body').css('background','url(include/hotel/images/no_bg.png) no-repeat center');
                    }else{
                        $('#total_indent').find('.modal-body').css('background','#f0f0f0');
                    }
                }

            } else{
                if($(".order-item").length == 1){
                    var this_status = $(".order-item").attr('data-status');     //获取改订单的状态
                    if(m == this_status || m == -1){     	 //如果该状态等于自己设定的状态
                        $(".order-item[data-status="+this_status+"]").css("display", "block"); //根据属性值获取元素
                    }else{
                        $(".order-item[data-status="+this_status+"]").css("display", "none"); //根据属性值获取元素
                    }
                    //退款中，退款成功的状态归为退款类
                    if(n == 4){
                        $(".order-item[data-status=4]").css("display", "block"); //根据属性值获取元素
                        $(".order-item[data-status=5]").css("display", "block"); //根据属性值获取元素
                    }

                    that.show_no_dingdan_bg(m); //显示背景图

                }else{
                    $(".order-item").siblings().hide();
                    $(".order-item[data-status="+m+"]").css("display", "block"); //根据属性值获取元素

                    //退款中，退款成功的状态归为退款类
                    if(n == 4){
                        $(".order-item[data-status=4]").css("display", "block"); //根据属性值获取元素
                        $(".order-item[data-status=5]").css("display", "block"); //根据属性值获取元素
                    }

                    that.show_no_dingdan_bg(m);  //显示背景图
                }
            }
        })
    },

    //onclick indent show content
    show_indent: function() {
        var that = this;
        $('#all_indent').on('click', function() {
            that.hide_delide_btn();
            $('.order-container').find(".order-item").show();
            $('#tablist dt').eq(0).addClass('active').siblings('dt').removeClass('active');

            if($(".order-item").length == 0){
                $('#total_indent').find('.modal-body').css('background','url(include/hotel/images/no_bg.png) no-repeat center');
            }else{
                $('#total_indent').find('.modal-body').css('background','#f0f0f0');
            }
        })

        $('#orderindex_ul li').on('click', function(index) {
            that.hide_delide_btn();
            var r = $(this).index();
            var rr = r + 1;
            if($(".order-item").length == 1){
                var this_status = $(".order-item").attr('data-status');     //获取改订单的状态
                if(r == this_status){     	 //如果该状态等于自己设定的状态
                    $(".order-item[data-status="+this_status+"]").css("display", "block"); //根据属性值获取元素
                }else{
                    $(".order-item[data-status="+this_status+"]").css("display", "none"); //根据属性值获取元素
                }
            }else{
                $(".order-item").siblings().hide();
                $(".order-item[data-status="+r+"]").css("display", "block"); //根据属性值获取元素

                //退款中，退款成功的状态归为退款类
                if(r == 3){
                    $(".order-item[data-status=4]").css("display", "block"); //根据属性值获取元素
                    $(".order-item[data-status=5]").css("display", "block"); //根据属性值获取元素
                }

                that.show_no_dingdan_bg(r);   //显示没订单的背景图；
            }

            $('#tablist dt').eq(rr).addClass('active').siblings('dt').removeClass('active');
        })
    },

    //test
    me: function () {
        console.log(11)
    },

    //查看详情&&退款
    check_page: function () {

        //点击订单
        $('.order-container').find('.order-item').on('click', function () {

           order_id = $(this).attr("order-id");

            var url = "hotel.php?mod=personal_center&act=detail_ajax&order_id=" + order_id;
            jQuery.ajax({
                "url": url,
                "type": "get",

                'dataType': 'json',
                "success": function (html) {
                    console.log(html);
                    $(".detail_indent_ID").html(html['order_number']);
                    $(".detail_indent_number").html(html['order_phone']);
                    $(".detail_indent_person").html(html['order_name']);
                    $(".order_number").html(html['order_number']);
                    $(".detail_indent_pay").html(html['order_payway']);
                    $(".detail_indent_is").html(html['order_have_bre']);
                    $(".outtime_msg").html(html['order_enddate']);
                    $(".night_num").html(html['night_num']);
                    $(".coupon_money").html(html['order_redpack_money']);

                },
                "error": function () {
                    alert("error");
                }
            });

            //初始化页面数据
            $('#c_payment_index_OrderDetail_box').find('.detail_indent_ID').text($(this).find('.title div').eq(0).text());  				 //ID
            $('#c_payment_index_OrderDetail_box').find('.detail_indent_status.special.big').text($(this).find('.title div').eq(1).text()); //订单状态
            $('#c_payment_index_OrderDetail_box').find('.hotel-bill-checkin-time').text($(this).find('.item').eq(0).text());  	         //时间
            $('#c_payment_index_OrderDetail_box').find('.cfont18.bold').text($(this).find('.item').eq(1).text());


            //总金额
        })

        /*模态框的返回*/
        //订单详情页面
        $('#to_detail_indent').on('hidden.bs.modal', function () {
            $('#total_indent').modal('show');
            /*setTimeout(function(){

                $('.order-container').find('.order-item').attr({"data-toggle":"modal","data-target":"#to_detail_indent"});
            },30)*/
        })

        /*模态框的返回*/
        //退款页面
        $('#to_refund').on('hidden.bs.modal', function () {
            $('#total_indent').modal('show');
            /*setTimeout(function(){

                $('.order-container').find('.order-item').attr({"data-toggle":"modal","data-target":"#to_detail_indent"});
            },30)*/
        })

        //点击订单里面的button
        $('.order-container').find('.order-item').find('.btn-container .mt-btn').on('click', function () {
            $('.order-container').find('.order-item').removeAttr('data-toggle data-target');

            var s = $(this).parent().parent().parent().parent().parent().attr("data-status");    //该订单的状态值
            if (s == 0) {        //go to pay
                console.log("跳转支付页面");
            } else if (s == 1) {   //go to refund page
                var str = $(this).parent().parent().find('.item').eq(1).text();   //返回该订单的父级
                $("#refund_box").find(".big.special").text(str.substr(3, 12));

            } else {    //go to detail page
                //初始化页面数据
                $('#c_payment_index_OrderDetail_box').find('.detail_indent_ID').text($(this).parent().parent().find('.title div').eq(0).text());  				 //ID
                $('#c_payment_index_OrderDetail_box').find('.detail_indent_status.special.big').text($(this).parent().parent().find('.title div').eq(1).text()); //订单状态
                $('#c_payment_index_OrderDetail_box').find('.hotel-bill-checkin-time').text($(this).parent().parent().find('.item').eq(0).text());  	         //时间
                $('#c_payment_index_OrderDetail_box').find('.cfont18.bold').text($(this).parent().parent().find('.item').eq(1).text());  	                     //总金额
            }
        })
    },

    //创建

    //查看会员，存储卡的内容
    check_content: function () {
        $('#huiyuan').find('.modal-body .list').find('.react').on('click', function () {
            var is_show = $(this).parent().find('.huiyuan_more_content').attr('data-is_show');
            if (is_show == 0) {
                $(this).parent().find('.huiyuan_more_content').attr('data-is_show', '1').css({
                    'opacity': '1',
                    'height': '4rem'
                });
            } else if (is_show == 1) {
                $(this).parent().find('.huiyuan_more_content').attr('data-is_show', '0').css({
                    'opacity': '0',
                    'height': '0'
                });
            }
        });

        $('#jifen').find('.modal-body .list').find('.react').on('click',function(){
            var is_show = $(this).parent().find('.huiyuan_more_content').attr('data-is_show');
            if(is_show == 0){
                $(this).parent().find('.huiyuan_more_content').attr('data-is_show','1').css({'opacity':'1','height':'4rem'});
            }else if(is_show == 1){
                $(this).parent().find('.huiyuan_more_content').attr('data-is_show','0').css({'opacity':'0','height':'0'});
            }
        })

        $('#storage').find('.modal-body .list').find('.react').on('click', function () {
            var is_show = $(this).parent().find('.storage_content').attr('data-is_show');
            if (is_show == 0) {
                $(this).parent().find('.storage_content').attr('data-is_show', '1').css({
                    'opacity': '1',
                    'height': '4rem'
                });
                $('.storage_record').find('.storage_money.orange.ellipsis').on('click', function () {
                    $('.storage_record').find('.storage_money.orange.ellipsis').removeClass('money');
                    $(this).addClass('money');
                })
            } else if (is_show == 1) {
                $(this).parent().find('.storage_content').attr('data-is_show', '0').css({
                    'opacity': '0',
                    'height': '0'
                });
                $('.storage_record').find('.storage_money.orange.ellipsis.money').removeClass('money');
            }
        })

    },

    //校验金额格式
    check_money_style: function () {
        //聚焦
        $('#storage_next_recharge').on('click', function () {
            $('#storage_recharge').on('shown.bs.modal', function () {
                $('#recharge_detail_money').focus();
            });
        })

        //点击下一步
        $('#recharge_next').on('click', function () {
            var recharge_detail_money = $('#recharge_detail_money').val();
            var is_num = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
            //判断格式是否正确
            if (!is_num.test(recharge_detail_money)) {
                alert("金额信息错误,请重新填写");
                $('#recharge_detail_money').val('');   //清空
            }else{
                var charge_money = $("#recharge_detail_money").val();
                window.location.href = "hotel.php?mod=personal_center&act=charge_moneycard&charge_money=" + charge_money;
            }
        })

    },

    //滑动删除
    touchmove_delide: function () {
        var that = this;
        $('.order-container').find('.order-item').bind('touchstart', function (e) {
            var obj = $(this);
            e = e || event;
            var startX = e.originalEvent.changedTouches[0].pageX;

            $(this).bind('touchmove', function (e) {
                e = e || event;
                //获取滑动屏幕时的X
                var endX = e.originalEvent.changedTouches[0].pageX;
                var distanceX = endX - startX;

                if (distanceX > 30) {
                    that.hide_delide_btn();
                    $(this).find('.hide_btn').css('right', '-1.4rem');
                    $(this).find('.order').css('right', '0');
                }
                else if (distanceX < -200) {
                    that.hide_delide_btn()
                    $(this).find('.hide_btn').css('right', '0');
                    $(this).find('.order').css('right', '1.4rem');

                    $(this).find('.hide_btn').click(function () {   //点击删除
                        $(this).parent().removeAttr('data-toggle data-target');
                        that.delide_decide_tip(obj);   //创建tip页面


                    })
                }

            });
        });
    },

    //删除确定提示
    delide_decide_tip: function (obj) {
        var that = this;
        if (that.once) {
            var this_status = obj.attr('data-status');  //该订单的状态
            var that = this;
            var str = that.create_tips_box();
            $('#create_tip').append(str);
            that.once = false;
            $('#create_tip').find('#tips_box').find('.decide_btn').on('click', function () {
                $('#create_tip').html('');  //清空
                obj.remove('.order-item');  //删除订单
                that.show_no_dingdan_bg(this_status);  //显示背景
                that.show_dingdan_num();
                that.once = true;

                var url = "hotel.php?mod=personal_center&act=delete_ajax&order_id=" + order_id;
                jQuery.ajax({
                    "url": url,
                    "type": "get",

                    'dataType': 'json',
                    "success": function (html) {
                        console.log("删除成功")
                    },
                    "error": function () {
                        alert("error");
                    }
                });
            });

            $('#create_tip').find('#tips_box').find('.cancal_btn').on('click', function () {
                that.hide_delide_btn();
                $('#create_tip').html('');  //清空
                $('.order-container').find('.order-item').attr({
                    "data-toggle": "modal",
                    "data-target": "#to_detail_indent"
                });
                that.once = true;
            })

        } else {
            return;
        }
    },

    //创建提示确定页面
    create_tips_box: function () {
        var str = '<div id="tips_mask"></div>' +
            '<div class="tips_box" id="tips_box">' +
            '<div class="tips_box_in">' +
            '<div class="tips_content">' +
            '<div class="tips_title">确定删除?</div>' +
            '<div class="tips_btn">' +
            '<div class="cancal_btn"> 取消</div>' +
            '<div class="decide_btn"> 确定</div>' +
            '</div>' +
            '</div>' +
            '</div>' +
            '</div>';
        return str;
    },

    //删除按钮隐藏
    hide_delide_btn: function () {
        $('.order-container').find('.order-item').find('.hide_btn').css('right', '-1.4rem');
        $('.order-container').find('.order-item').find('.order').css('right', '0');
    },




    //显示订单数量
    show_dingdan_num:function(){
        var li_status_0 = $('.order-container').find('.order-item[data-status=0]').length;  //数量
        var li_status_1 = $('.order-container').find('.order-item[data-status=1]').length;  //数量
        var li_status_2 = $('.order-container').find('.order-item[data-status=2]').length;  //数量
        var li_status_3 = $('.order-container').find('.order-item[data-status=3]').length;  //数量
        var li_status_4 = $('.order-container').find('.order-item[data-status=4]').length;  //数量
        var li_status_5 = $('.order-container').find('.order-item[data-status=5]').length;  //数量
        var li_status = li_status_3+li_status_4+li_status_5;

        if(li_status_0 == 0){
            $('#orderindex_ul li').eq(0).find('span').eq(1).css('display','none');
        }else{
            $('#orderindex_ul li').eq(0).find('span').eq(1).css('display','block').text(li_status_0);
        }

        if(li_status_1 == 0){
            $('#orderindex_ul li').eq(1).find('span').eq(1).css('display','none');
        }else{
            $('#orderindex_ul li').eq(1).find('span').eq(1).css('display','block').text(li_status_1);
        }

        if(li_status_2 == 0){
            $('#orderindex_ul li').eq(2).find('span').eq(1).css('display','none');
        }else{
            $('#orderindex_ul li').eq(2).find('span').eq(1).css('display','block').text(li_status_2);
        }

        if(li_status == 0){
            $('#orderindex_ul li').eq(3).find('span').eq(1).css('display','none');
        }else{
            $('#orderindex_ul li').eq(3).find('span').eq(1).css('display','block').text(li_status);
        }
    },


    //显示没有订单时的背景图片     //传订单状态判断
    show_no_dingdan_bg:function(data_status){
        var dingdan_length = $(".order-item[data-status="+data_status+"]").length ;  //订单数量
        if(data_status == 3){
            var dingdan_4 = $(".order-item[data-status=4]").length ;         //订单状态 3与4，5 绑定
            var dingdan_5 = $(".order-item[data-status=5]").length ;
            var dingdan = dingdan_length + dingdan_4 + dingdan_5;
            if(dingdan == 0){
                $('#total_indent').find('.modal-body').css('background','url(include/hotel/images/no_bg.png) no-repeat center');
            }else{
                $('#total_indent').find('.modal-body').css('background','#f0f0f0');
            }
        }else{
            if(dingdan_length == 0){
                $('#total_indent').find('.modal-body').css('background','url(include/hotel/images/no_bg.png) no-repeat center');
            }else{
                $('#total_indent').find('.modal-body').css('background','#f0f0f0');
            }
        }
    },



    use_hongbao:function(){
        $('#modalLarge').find('.moneycard .at_once').on('click',function(){
            window.location.href = "hotel.php?mod=index";
        })
    }

}